﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_MaterialOrder : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public string order_number;
        
        public frm_Edit_MaterialOrder()
        {
            InitializeComponent();
            this.order_number = null;
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.orderDate.EditValue = DateTime.Now.Date;
        }

        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(order_number))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [444_MaterialOrder] WHERE OrderNo='" + order_number + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.orderNo.Text = dataReader["OrderNo"].ToString();
                        if (dataReader["OrderDate"] != DBNull.Value)
                        {
                            this.orderDate.EditValue = DateTime.Parse(dataReader["OrderDate"].ToString());
                        }
                        if (dataReader["DeadLine"] != DBNull.Value)
                        {
                            this.deadLine.EditValue = DateTime.Parse(dataReader["DeadLine"].ToString());
                        }

                        if (dataReader["ExpiredDate"] != DBNull.Value)
                        {
                            this.expiredDate.EditValue = DateTime.Parse(dataReader["ExpiredDate"].ToString());
                        }

                        this.invoiceValue.Text = dataReader["Invoice"].ToString();
                        this.remarkValue.Text = dataReader["Remark"].ToString();
                    }
                    else
                    {
                        ret = false;
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }
            return ret;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void orderNo_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            this.orderNo.SelectAll();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DateTime order_date = new DateTime();
            DateTime dead_line = new DateTime();
            DateTime expired_date = new DateTime();

            string error = null;
            string order = null;

            if (string.IsNullOrEmpty(this.orderNo.Text))
            {
                error = "Please enter Order No";
            }
            else
            {
                order = this.orderNo.Text;
                if (order.Length != 6)
                {
                    error = "Invalid Order Number";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.orderDate.EditValue != null)
                {
                    order_date = DateTime.Parse(this.orderDate.EditValue.ToString());
                }

                if (this.deadLine.EditValue != null)
                {
                    dead_line = DateTime.Parse(this.deadLine.EditValue.ToString());
                }

                if (this.expiredDate.EditValue != null)
                {
                    expired_date = DateTime.Parse(this.expiredDate.EditValue.ToString());
                }

                string invoice = this.invoiceValue.Text;
                string remark = this.remarkValue.Text;

                try
                {                    
                    myConnection.Open();
                    string sql = "UPDATE [444_MaterialOrder] SET DeadLine=@deadline, OrderDate=@orderdate,"
                        + " ExpiredDate=@expireddate, Invoice='" + invoice + "', Remark='" + remark + "'"
                        + " WHERE OrderNo='" + order + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@deadline", SqlDbType.DateTime).Value = dead_line.Date;
                    cmd_sql.Parameters.Add("@orderdate", SqlDbType.DateTime).Value = order_date.Date;
                    cmd_sql.Parameters.Add("@expireddate", SqlDbType.DateTime).Value = expired_date.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Material Orther updated success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Material Order. Please try again.");
                }
            }
        }
    }
}
